/*
  Copyright 2009 Smart Enterprise Solution Corp.
  Email: contact@ses.vn - Website: http://www.ses.vn
  KimNgan Project.
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Data.SqlClient;
using SES.CMS.DO;
/// <summary>
/// Summary description for cmsCustomerDAL
/// </summary>
namespace SES.CMS.DAL
{

    public class cmsCustomerDAL : BaseDAL
    {
        #region Private Variables

        #endregion

        #region Public Constructors
        public cmsCustomerDAL()
        {
            //
            // TODO: Add constructor logic here
            //

        }
        #endregion



        #region Public Methods
        public int Insert(cmsCustomerDO objcmsCustomerDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsCustomer_Insert";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@FullName", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsCustomerDO.FullName;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@UserName", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsCustomerDO.UserName;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Password", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsCustomerDO.Password;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Sex", SqlDbType.Bit);
            Sqlparam.Value = objcmsCustomerDO.Sex;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Brithday", SqlDbType.DateTime);
            Sqlparam.Value = objcmsCustomerDO.Brithday;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Email", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsCustomerDO.Email;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Phone", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsCustomerDO.Phone;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Address", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsCustomerDO.Address;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@RegisterDay", SqlDbType.DateTime);
            Sqlparam.Value = objcmsCustomerDO.RegisterDay;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@IsEnable", SqlDbType.Bit);
            Sqlparam.Value = objcmsCustomerDO.IsEnable;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@IsVangLai", SqlDbType.Bit);
            Sqlparam.Value = objcmsCustomerDO.IsVangLai;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ID", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);


            int result = base.ExecuteNoneQuery(Sqlcomm);

            if (!Convert.IsDBNull(Sqlcomm.Parameters["@ID"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ID"].Value);

            return result;
        }

        public int Update(cmsCustomerDO objcmsCustomerDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsCustomer_UpdateByPK";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@Id", SqlDbType.Int);
            Sqlparam.Value = objcmsCustomerDO.Id;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@FullName", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsCustomerDO.FullName;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@UserName", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsCustomerDO.UserName;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Password", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsCustomerDO.Password;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Sex", SqlDbType.Bit);
            Sqlparam.Value = objcmsCustomerDO.Sex;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Brithday", SqlDbType.DateTime);
            Sqlparam.Value = objcmsCustomerDO.Brithday;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Email", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsCustomerDO.Email;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Phone", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsCustomerDO.Phone;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Address", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsCustomerDO.Address;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@RegisterDay", SqlDbType.DateTime);
            Sqlparam.Value = objcmsCustomerDO.RegisterDay;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@IsEnable", SqlDbType.Bit);
            Sqlparam.Value = objcmsCustomerDO.IsEnable;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@IsVangLai", SqlDbType.Bit);
            Sqlparam.Value = objcmsCustomerDO.IsVangLai;
            Sqlcomm.Parameters.Add(Sqlparam);



            Sqlparam = new SqlParameter("@ErrorCode", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);

            int result = base.ExecuteNoneQuery(Sqlcomm);

            if (!Convert.IsDBNull(Sqlcomm.Parameters["@ErrorCode"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ErrorCode"].Value);

            return result;


        }

        public int Delete(cmsCustomerDO objcmsCustomerDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsCustomer_DeleteByPK";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@Id", SqlDbType.Int);
            Sqlparam.Value = objcmsCustomerDO.Id;
            Sqlcomm.Parameters.Add(Sqlparam);



            int result = base.ExecuteNoneQuery(Sqlcomm);
            return result;
        }

        public int DeleteAll()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsCustomer_DeleteAll";

            int result = base.ExecuteNoneQuery(Sqlcomm);
            return result;
        }

        public cmsCustomerDO Select(cmsCustomerDO objcmsCustomerDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsCustomer_GetByPK";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@Id", SqlDbType.Int);
            Sqlparam.Value = objcmsCustomerDO.Id;
            Sqlcomm.Parameters.Add(Sqlparam);



            DataSet ds = base.GetDataSet(Sqlcomm);
            DataRow dr = null;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                dr = ds.Tables[0].Rows[0];
                if (!Convert.IsDBNull(dr["Id"]))
                    objcmsCustomerDO.Id = Convert.ToInt32(dr["Id"]);
                if (!Convert.IsDBNull(dr["FullName"]))
                    objcmsCustomerDO.FullName = Convert.ToString(dr["FullName"]);
                if (!Convert.IsDBNull(dr["UserName"]))
                    objcmsCustomerDO.UserName = Convert.ToString(dr["UserName"]);
                if (!Convert.IsDBNull(dr["Password"]))
                    objcmsCustomerDO.Password = Convert.ToString(dr["Password"]);
                if (!Convert.IsDBNull(dr["Sex"]))
                    objcmsCustomerDO.Sex = Convert.ToBoolean(dr["Sex"]);
                if (!Convert.IsDBNull(dr["Brithday"]))
                    objcmsCustomerDO.Brithday = Convert.ToDateTime(dr["Brithday"]);
                if (!Convert.IsDBNull(dr["Email"]))
                    objcmsCustomerDO.Email = Convert.ToString(dr["Email"]);
                if (!Convert.IsDBNull(dr["Phone"]))
                    objcmsCustomerDO.Phone = Convert.ToString(dr["Phone"]);
                if (!Convert.IsDBNull(dr["Address"]))
                    objcmsCustomerDO.Address = Convert.ToString(dr["Address"]);
                if (!Convert.IsDBNull(dr["RegisterDay"]))
                    objcmsCustomerDO.RegisterDay = Convert.ToDateTime(dr["RegisterDay"]);
                if (!Convert.IsDBNull(dr["IsEnable"]))
                    objcmsCustomerDO.IsEnable = Convert.ToBoolean(dr["IsEnable"]);
                if (!Convert.IsDBNull(dr["IsVangLai"]))
                    objcmsCustomerDO.IsVangLai = Convert.ToBoolean(dr["IsVangLai"]);

            }
            return objcmsCustomerDO;
        }

        public cmsCustomerDO SelectByUsername(cmsCustomerDO objcmsCustomerDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsCustomer_GetByUserName";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@UserName", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsCustomerDO.UserName;
            Sqlcomm.Parameters.Add(Sqlparam);



            DataSet ds = base.GetDataSet(Sqlcomm);
            DataRow dr = null;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                dr = ds.Tables[0].Rows[0];
                if (!Convert.IsDBNull(dr["Id"]))
                    objcmsCustomerDO.Id = Convert.ToInt32(dr["Id"]);
                if (!Convert.IsDBNull(dr["FullName"]))
                    objcmsCustomerDO.FullName = Convert.ToString(dr["FullName"]);
                if (!Convert.IsDBNull(dr["UserName"]))
                    objcmsCustomerDO.UserName = Convert.ToString(dr["UserName"]);
                if (!Convert.IsDBNull(dr["Password"]))
                    objcmsCustomerDO.Password = Convert.ToString(dr["Password"]);
                if (!Convert.IsDBNull(dr["Sex"]))
                    objcmsCustomerDO.Sex = Convert.ToBoolean(dr["Sex"]);
                if (!Convert.IsDBNull(dr["Brithday"]))
                    objcmsCustomerDO.Brithday = Convert.ToDateTime(dr["Brithday"]);
                if (!Convert.IsDBNull(dr["Email"]))
                    objcmsCustomerDO.Email = Convert.ToString(dr["Email"]);
                if (!Convert.IsDBNull(dr["Phone"]))
                    objcmsCustomerDO.Phone = Convert.ToString(dr["Phone"]);
                if (!Convert.IsDBNull(dr["Address"]))
                    objcmsCustomerDO.Address = Convert.ToString(dr["Address"]);
                if (!Convert.IsDBNull(dr["RegisterDay"]))
                    objcmsCustomerDO.RegisterDay = Convert.ToDateTime(dr["RegisterDay"]);
                if (!Convert.IsDBNull(dr["IsEnable"]))
                    objcmsCustomerDO.IsEnable = Convert.ToBoolean(dr["IsEnable"]);
                if (!Convert.IsDBNull(dr["IsVangLai"]))
                    objcmsCustomerDO.IsVangLai = Convert.ToBoolean(dr["IsVangLai"]);

            }
            return objcmsCustomerDO;
        }

        public ArrayList SelectAll1()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsCustomer_GetAll";

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;
            ArrayList arrcmsCustomerDO = new ArrayList();
            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    cmsCustomerDO objcmsCustomerDO = new cmsCustomerDO();
                    if (!Convert.IsDBNull(dr["Id"]))
                        objcmsCustomerDO.Id = Convert.ToInt32(dr["Id"]);
                    if (!Convert.IsDBNull(dr["FullName"]))
                        objcmsCustomerDO.FullName = Convert.ToString(dr["FullName"]);
                    if (!Convert.IsDBNull(dr["UserName"]))
                        objcmsCustomerDO.UserName = Convert.ToString(dr["UserName"]);
                    if (!Convert.IsDBNull(dr["Password"]))
                        objcmsCustomerDO.Password = Convert.ToString(dr["Password"]);
                    if (!Convert.IsDBNull(dr["Sex"]))
                        objcmsCustomerDO.Sex = Convert.ToBoolean(dr["Sex"]);
                    if (!Convert.IsDBNull(dr["Brithday"]))
                        objcmsCustomerDO.Brithday = Convert.ToDateTime(dr["Brithday"]);
                    if (!Convert.IsDBNull(dr["Email"]))
                        objcmsCustomerDO.Email = Convert.ToString(dr["Email"]);
                    if (!Convert.IsDBNull(dr["Phone"]))
                        objcmsCustomerDO.Phone = Convert.ToString(dr["Phone"]);
                    if (!Convert.IsDBNull(dr["Address"]))
                        objcmsCustomerDO.Address = Convert.ToString(dr["Address"]);
                    if (!Convert.IsDBNull(dr["RegisterDay"]))
                        objcmsCustomerDO.RegisterDay = Convert.ToDateTime(dr["RegisterDay"]);
                    if (!Convert.IsDBNull(dr["IsEnable"]))
                        objcmsCustomerDO.IsEnable = Convert.ToBoolean(dr["IsEnable"]);
                    if (!Convert.IsDBNull(dr["IsVangLai"]))
                        objcmsCustomerDO.IsVangLai = Convert.ToBoolean(dr["IsVangLai"]);
                    arrcmsCustomerDO.Add(objcmsCustomerDO);
                }
            }
            return arrcmsCustomerDO;
        }

        public DataTable SelectAll()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsCustomer_GetAll";

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;
        }
        public DataTable SelectLogin(string txtUsername, string txtPassword)
        {
            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsCustomer_Login";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@Username", SqlDbType.NVarChar);
            Sqlparam.Value = txtUsername;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Password", SqlDbType.NVarChar);
            Sqlparam.Value = txtPassword;
            Sqlcomm.Parameters.Add(Sqlparam);


            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;
        }
        public DataTable SelectRegistration(string txtUsername)
        {
            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsCustomer_SelectRegistration";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@Username", SqlDbType.NVarChar);
            Sqlparam.Value = txtUsername;
            Sqlcomm.Parameters.Add(Sqlparam);

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;
        }
        public DataTable SelectEmail(string txtEmail)
        {
            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsCustomer_SelectEmail";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@Email", SqlDbType.NVarChar);
            Sqlparam.Value = txtEmail;
            Sqlcomm.Parameters.Add(Sqlparam);

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;
        }
        public DataTable GetByIsVangLai(bool IsVangLai)
        {
            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsCustomer_GetByIsVangLai";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@IsVangLai", SqlDbType.Bit);
            Sqlparam.Value = IsVangLai;
            Sqlcomm.Parameters.Add(Sqlparam);

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;
        }
        #endregion

    }

}
